<template>
	<el-dialog v-model="isShowDialog" :title="'订单明细'" :width="1200" draggable="">
		<div class="busSubOrder-container">
			<el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
				<el-form :model="queryParams" ref="queryForm" :inline="true">
					<el-form-item label="商品名称">
						<el-select clearable="" filterable="" v-model="queryParams.goodName" placeholder="请选择商品名称">
							<el-option v-for="(item, index) in busGoodsDropdownList" :key="index" :value="item.value" :label="item.label" />
						</el-select>
					</el-form-item>
					<el-form-item label="商品编码">
						<el-input v-model="queryParams.goodCode" clearable="" placeholder="请输入商品编码" />
					</el-form-item>
					<el-form-item label="订单状态">
						<el-select clearable="" v-model="queryParams.status" placeholder="请选择订单状态">
							<el-option v-for="(item, index) in getstatusData" :key="index" :value="item.value" :label="item.label" />
						</el-select>
					</el-form-item>
					<el-form-item>
						<el-button-group>
							<el-button type="primary" icon="ele-Search" @click="handleQuery" v-auth="'busSubOrder:page'"> 查询 </el-button>
							<el-button icon="ele-Refresh" @click="() => (queryParams = {})"> 重置 </el-button>
						</el-button-group>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" icon="ele-Plus" @click="openAddBusSubOrder" v-auth="'busSubOrder:add'"> 新增 </el-button>
					</el-form-item>
				</el-form>
			</el-card>
			<el-card class="full-table" shadow="hover" style="margin-top: 8px">
				<el-table :data="tableData" style="width: 100%" v-loading="loading" tooltip-effect="light" row-key="id" border="">
					<el-table-column type="index" label="序号" width="55" align="center" />
					<el-table-column prop="id" label="主键Id" show-overflow-tooltip="" />
					<el-table-column prop="orderCode" label="订单号" show-overflow-tooltip="" />

					<el-table-column prop="subOrderCode" label="子订单号" show-overflow-tooltip="" />
					<!-- <el-table-column prop="tenantId" label="租户Id"   show-overflow-tooltip="" /> -->
					<el-table-column prop="busStroeSName" label="门店" show-overflow-tooltip="" />
					<el-table-column prop="storeCode" label="门店代码" show-overflow-tooltip="">
						<!-- <template #default="scope">
            <span>{{scope.row.busStroeSName}}</span>
            
          </template>
           -->
					</el-table-column>
					<el-table-column prop="goodName" label="商品名称" show-overflow-tooltip="" />

					<el-table-column prop="goodCode" label="商品编码" show-overflow-tooltip="" />
					<el-table-column prop="quantity" label="数量" show-overflow-tooltip="" />
					<el-table-column prop="saleamount" label="订单原金额" show-overflow-tooltip="" />
					<el-table-column prop="realamount" label="实际支付金额" show-overflow-tooltip="" />
					<el-table-column prop="payamount" label="实际支付金额" show-overflow-tooltip="" />
					<el-table-column prop="discountamount" label="折扣金额" show-overflow-tooltip="" />
					<el-table-column prop="unit" label="单位（量纲）" show-overflow-tooltip="" />
					<el-table-column prop="discountRemark" label="折扣说明" show-overflow-tooltip="" />
					<el-table-column prop="remark" label="订单备注" show-overflow-tooltip="" />
					<el-table-column prop="status" label="订单状态" show-overflow-tooltip="" />
					<el-table-column prop="createTime" label="创建时间" show-overflow-tooltip="" />
					<el-table-column prop="updateTime" label="更新时间" show-overflow-tooltip="" />
					<el-table-column prop="createUserId" label="创建者Id" show-overflow-tooltip="" />
					<el-table-column prop="updateUserId" label="修改者Id" show-overflow-tooltip="" />
					<!-- <el-table-column prop="isDelete" label="软删除" show-overflow-tooltip="">
          <template #default="scope">
            <el-tag v-if="scope.row.isDelete"> 是 </el-tag>
            <el-tag type="danger" v-else=""> 否 </el-tag>
            
          </template>
          
        </el-table-column> -->
					<el-table-column label="操作" width="140" align="center" fixed="right" show-overflow-tooltip="">
						<template #default="scope">
							<el-button icon="ele-Edit" size="small" text="" type="primary" @click="openEditBusSubOrder(scope.row)" v-auth="'busSubOrder:edit'"> 编辑 </el-button>
							<el-button icon="ele-Delete" size="small" text="" type="primary" @click="delBusSubOrder(scope.row)" v-auth="'busSubOrder:delete'"> 删除 </el-button>
						</template>
					</el-table-column>
				</el-table>
				<el-pagination
					v-model:currentPage="tableParams.page"
					v-model:page-size="tableParams.pageSize"
					:total="tableParams.total"
					:page-sizes="[10, 20, 50, 100]"
					small=""
					background=""
					@size-change="handleSizeChange"
					@current-change="handleCurrentChange"
					layout="total, sizes, prev, pager, next, jumper"
				/>
				<editDialog ref="editDialogRef" :title="editBusSubOrderTitle" @reloadTable="handleQuery" />
			</el-card>
		</div>
	</el-dialog>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import type { FormRules } from 'element-plus';
import editDialog from '/@/views/main/busSubOrder/component/editDialog.vue';

import { pageBusSubOrder, deleteBusSubOrder } from '/@/api/main/busSubOrder';
import { getBusGoodsDropdown } from '/@/api/main/busSubOrder';

const isShowDialog = ref(false);

const openDialog = async (row: any) => {
	isShowDialog.value = true;
	console.log(row.orderCode);
	loading.value = true;
	var res = await pageBusSubOrder(Object.assign(tableParams.value, { orderCode: row.orderCode }));
	tableData.value = res.data.result?.items ?? [];
	console.log(res.data.result.items);
	tableParams.value.total = res.data.result?.total;
	loading.value = false;
};

const getstatusData = ref<any>([]);

const editDialogRef = ref();
const loading = ref(false);
const tableData = ref<any>([]);
const queryParams = ref<any>({});
const tableParams = ref({
	page: 1,
	pageSize: 10,
	total: 0,
});
const editBusSubOrderTitle = ref('');

// 查询操作
const handleQuery = async () => {
	loading.value = true;
	var res = await pageBusSubOrder(Object.assign(queryParams.value, tableParams.value));
	tableData.value = res.data.result?.items ?? [];
	tableParams.value.total = res.data.result?.total;
	loading.value = false;
	// getstatusData.value = await dictTypeDataList('');
};

// 打开新增页面
const openAddBusSubOrder = () => {
	editBusSubOrderTitle.value = '添加订单详细';
	editDialogRef.value.openDialog({});
};

// 打开编辑页面
const openEditBusSubOrder = (row: any) => {
	editBusSubOrderTitle.value = '编辑订单详细';
	editDialogRef.value.openDialog(row);
};

// 删除
const delBusSubOrder = (row: any) => {
	ElMessageBox.confirm(`确定要删除吗?`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			await deleteBusSubOrder(row);
			handleQuery();
			ElMessage.success('删除成功');
		})
		.catch(() => {});
};

// 改变页面容量
const handleSizeChange = (val: number) => {
	tableParams.value.pageSize = val;
	handleQuery();
};

// 改变页码序号
const handleCurrentChange = (val: number) => {
	tableParams.value.page = val;
	handleQuery();
};

const busGoodsDropdownList = ref<any>([]);
const getBusGoodsDropdownList = async () => {
	let list = await getBusGoodsDropdown();
	busGoodsDropdownList.value = list.data.result ?? [];
};

getBusGoodsDropdownList();

// const dictTypeDataList = async (val: any) => {
//   let list = await getDictDataList(val);
//   return list.data.result ?? [];
// };
// handleQuery();

defineExpose({ openDialog });
</script>



